// 引入 express 模块
const express = require('express');
const app = express();

app.use(express.json());
// 设置允许跨域资源共享的中间件
app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
    res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    next();
});
class TWebApiServer {
    constructor() {
        this.port = 3000;
        this.sipService = (require("./sip")).sipService;
    }
    start() {
        var me = this;
        return new Promise((su, fa) => {
            try {
                // 监听 this.port 端口
                app.listen(this.port, () => {
                    console.log('Server running on http://localhost:' + this.port);
                    me.sipService.start().then(_ => {});
                    su();
                });
            } catch (error) {
                console.log(error)
                su();
            }
        })
    }
    registRoute(routePath, handler) {
        var me = this;
        try {
            if (routePath.indexOf("/") != 0) {
                routePath = "/" + routePath;
            }
            app.all(routePath, (req, res) => {
                var result = {
                    status: 0,
                    routePath: routePath,
                    resData: null,
                    resError: null
                };
                handler(req, res, result).then(_ => {
                    res.json(result)
                })
            })
        } catch (error) {
            console.log(error);
        }
    }
}


const sipWebSvr = new TWebApiServer();
sipWebSvr.registRoute("/sip/version", (req, res, result) => {
    return new Promise((su, fa) => {
        try {
            result.status = 1;
            result.resData = {
                version: sipWebSvr.sipService.version
            }
            su();
        } catch (error) {
            console.log(error);
            su();
        }
    })
});
sipWebSvr.registRoute("/sip/querydevices", (req, res, result) => {
    return new Promise((su, fa) => {
        try {
            var deviceUri = req.body["deviceuri"] || req.query["deviceuri"] || req.params["deviceuri"];
            sipWebSvr.sipService.queryDeviceList(deviceUri);
            result.status = 1;
            result.resData = {
                uri: deviceUri
            }
            su();
        } catch (error) {
            console.log(error);
            su();
        }
    })
});
sipWebSvr.registRoute("/sip/invite", (req, res, result) => {
    return new Promise((su, fa) => {
        try {
            var channelCode = req.body["channelCode"] || req.query["channelCode"] || req.params["channelCode"];
            var flag = sipWebSvr.sipService.inviteChannel(channelCode);
            result.status = flag ? 1 : 0;
            result.resData = {
                channelCode
            }
            su();
        } catch (error) {
            console.log(error);
            su();
        }
    })
});
sipWebSvr.registRoute("/sip/getSessions", (req, res, result) => {
    return new Promise((su, fa) => {
        try {
            result.status = 1;
            result.resData = {
                sessions: sipWebSvr.sipService.sessions
            }
            su();
        } catch (error) {
            console.log(error);
            su();
        }
    })
});
sipWebSvr.registRoute("/sip/getChannels", (req, res, result) => {
    return new Promise((su, fa) => {
        try {
            result.status = 1;
            result.resData = {
                channels: sipWebSvr.sipService.channels
            }
            su();
        } catch (error) {
            console.log(error);
            su();
        }
    })
})
module.exports = {
    TWebApiServer,
    sipWebSvr
}